package com.caiyi.tips.user.controller;

import com.caiyi.tips.core.VO.ResultVO;
import com.caiyi.tips.core.enums.ResultEnum;
import com.caiyi.tips.core.util.CheckUtil;
import com.caiyi.tips.core.util.StringUtil;
import com.caiyi.tips.user.DTO.SmsDTO;
import com.caiyi.tips.user.exception.SmsException;
import com.caiyi.tips.user.exception.UserException;
import com.caiyi.tips.user.service.SmsService;
import com.caiyi.tips.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/*
 * @author Cliff.ZhouYang
 * @date 2017/11/20 14:55
 */
@RestController
@Api(value = "短信接口", tags = "3.短信接口", description = "短信相关接口", protocols = "http")
public class SmsController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private UserService userService;

    @PostMapping("/sendSms")
    public ResultVO sendSms(SmsDTO dto) {
        if (dto.getYzmType() == null) {
            throw new SmsException("验证码类型不能为空");
        }
        if (StringUtil.isEmpty(dto.getPhone())) {
            throw new SmsException("手机号为空");
        }
        if (!CheckUtil.isMobilephone(dto.getPhone())) {
            throw new SmsException("手机号格式不正确");
        }
        return smsService.sendYzm(dto);
    }

    @PostMapping(value = "/getPhoneMsg")
    @ApiOperation(value = "获取短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="imgYzm", value = "图形验证码", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name="phone", value = "手机号码", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name="userType", value = "用户类型", required = true, dataType = "int", paramType = "body"),
            @ApiImplicitParam(name="yzmType", value = "短信类型", required = true, dataType = "int", paramType = "body")
    })
    public ResultVO  getPhoneMsg(HttpServletRequest request, SmsDTO dto) {//0专家 1普通用户
     /*   if (dto.getUserType() == null) {
            throw new UserException(ResultEnum.USER_TYPE_ERROR.getCode(),"用户类型不能为空");
        }*/
        if (dto.getUserType() != 1 && dto.getUserType() != 0) {
            throw new UserException(ResultEnum.USER_TYPE_ERROR.getCode(),"用户类型不正确");
        }
        if (StringUtil.isEmpty(dto.getImgYzm())) {
            throw new UserException(ResultEnum.USER_TYPE_ERROR.getCode(),"用户类型不能为空");
        }
        HttpSession localHttpSession = request.getSession(true);
        String checkRand = String.valueOf(localHttpSession.getAttribute("rand"));
        if (StringUtils.isEmpty(dto.getPhone())) {
            throw new UserException(ResultEnum.USER_PHONE_ERROR.getCode(),"请输入手机号");
        }
        if(dto.getPhone().length() != 11){
            throw new UserException(ResultEnum.USER_PHONE_ERROR.getCode(),"手机号不得少于11位，请重新输入");
        }
        if (!CheckUtil.isMobilephone(dto.getPhone())) {
            throw new UserException(ResultEnum.USER_PHONE_ERROR.getCode(), "无效手机号，请重新输入");
        }
        int count =Integer.valueOf(localHttpSession.getAttribute("randErrCount").toString()) ;
        count++;
        localHttpSession.setAttribute("randErrCount", count);
        if(!dto.getImgYzm().equalsIgnoreCase(checkRand)){
            throw new UserException(ResultEnum.USER_RAND_ERROR.getCode(),"图形验证码不正确，请重新输入");
        }
        if(count>3){
            throw new UserException(ResultEnum.USER_RAND_INVALID_ERROR.getCode(),"图形验证码失效，请重新输入");
        }
        boolean flag = userService.isExistPhone(dto.getPhone(), dto.getUserType() );
        if(dto.getYzmType() ==0 && flag){
            throw new UserException(ResultEnum.USER_PHONE_ERROR.getCode(), "已注册手机号，请重新输入");
        }
        if(dto.getYzmType()  ==1 && !flag){
            throw new UserException(ResultEnum.USER_PHONE_ERROR.getCode(), "未注册手机号，请重新输入");
        }
        return smsService.sendYzm(dto);
    }
}
